<?php
namespace app\api\controller\approval;
use think\File;
use think\facade\Env;
use think\facade\Cache;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\rebate\RebateList;
use app\common\model\rebate\Rebate as RebateModel;
use app\api\controller\Base;
use app\api\controller\Baseuser;
use app\common\model\revenue_ap\Raplist as RapListModel;
use app\common\model\revenue_ap\Rap as RapModel;
use app\common\model\assay\Assaylist;
use app\common\model\company\Contract;
use app\common\model\approval\Approvalrole;
use app\common\model\approval\Approvallog as ApprovalLog;
use app\api\controller\mytool\Sendmessage;
use app\common\model\company\Contract as Contractmodel;


//审批申请表
class Pub extends Baseuser
{
	//初始化
    public function initialize(){
    	parent::initialize();
		// Rolepower::powerguard(101004);
	}
	
	//需要我审批的订单
	public function getlist(){
		$data = input('get.');
        $data = init_where($data,['time'=>'CreateTime']);
        $list = ApprovalLog::usergetlist($data);
        $filename = ['createuserinfo'];
        $typelist = ['contract'=>'合同','assay'=>'样品报价'];
        foreach ($list['list'] as $key => $value) {
        	$list['list'][$key]['Id'] = $value['CorrelationId'].'&type='.$value['Type'];
        	$list['list'][$key]['Typetext'] = $typelist[$value['Type']];
        	foreach ($filename as $fkey => $fvalue) {
        		if (empty($value[$fvalue]['Name'])) {
	                 $list['list'][$key][$fvalue.'Name'] = '暂无';
	            } else {
	                $list['list'][$key][$fvalue.'Name'] = $value[$fvalue]['Name'];
	                unset($list['list'][$key][$fvalue]);
	            }
        	}
        }
        $list['tr']['title']  = ['申请人','创建时间','订单状态','查看','订单分类'];
		$list['tr']['key'] = ['createuserinfoName','CreateTime','State','Id','Typetext'];
		$list['tr']['show']=[true,true,true,true,true];
		
		//我申请的 1  需要我审批的 2  3我负责的
		$list['tr']['type'] = [['Id'=>'1','Text'=>'我申请的'],['Id'=>'2','Text'=>'需要我审批的'],['Id'=>'3','Text'=>'我负责的']];
		$list['tr']['typekey'] = ['id'=>'Id','text'=>'Text'];
		$list['option']['type_select'] = 'radio';
        return myok($list);		
	}

	/**
	 * 获取送样详细信息 getreinfo
	 * @return [type] [description]
	 */
	public function getinfo(){
		$idata = input('get.');
		if(empty($idata['Type']) || empty($idata['Id'])){
			return myerr('缺少参数，请重新尝试');
		}
		$data = ApprovalLog::type_id_getinfoModel($idata['Type'],$idata['Id']);
		if(empty($data)){
			return myok('暂无创建此审核订单，是否直接创建？','','empty');
		}
		//获取关联信息
		switch ($data['Type']) {
			case 'assay':
				$lsdata['CompanyId'] = Assaylist::get($data['CorrelationId'])->value('CompanyId');
				break;
			case 'contract':
				$lsdata['CompanyId'] = Contractmodel::get($data['CorrelationId'])->value('CompanyId');
				break;
			default:
				// code...
				break;
		}
		if(!empty($lsdata['CompanyId'])){
			$data['companyinfo'] = Company::get($lsdata['CompanyId'])->visible(['Name','Address']);
		}
		$data['UserId'] = $this->user_id;
		return myok($data);
	}

	/**
	 * 创建审批订单,通知相应的审批负责人
	 * @return [type] [description]
	 */
	public function create(){
		$idata = input('get.');
		if(empty($idata['Type']) || empty($idata['Id'])){
			return myerr('缺少参数，请重新尝试');
		}
		$idata['CorrelationId'] = $idata['Id'];
		$CreateUserId = '';
		switch ($idata['Type']) {
			case 'contract':
				$sb = Contract::get($idata['Id']);
				$CreateUserId = $sb->BussinessId;
				break;
			case 'assay':
				$sb = Assaylist::get($idata['Id']);
				$CreateUserId = $sb->SubUserId;
				break;
		}
		if($CreateUserId!==$this->user_id){
			return moerr('关联订单并非您的订单，无法创建审批订单');
		}
		$id = ApprovalLog::user_create($idata);
		//通知审批人
		$sbdata = ApprovalLog::get($id);
		$sbdata['RulesPlan'] = json_decode($sbdata['RulesPlan'],true);
		foreach ($sbdata['RulesPlan']['Rules'] as $key => $value) {
			if($value['Step']!=='-1'){
				if($value['Type']=='会签'){
					$userids = $value['UserIds'];
				}else{
					$userids = $value['UserIds'][0];
				}
				break;//只循环第一个
			}
		}
		Sendmessage::approval(1,$sbdata,$userids);
		return myok($id);
	}

	/**
	 * 提示审批
	 * @return [type] [description]
	 */
	public function tip(){
		$idata = input('post.');
		if(empty($idata['Step'])|| empty($idata['Id'])){
			return myerr('缺少参数，请重新尝试');
		}
		$sb = Cache::get('Id'.$idata['Id'].'Step'.$idata['Step'].'ListIndex'.$idata['ListIndex']);
		if(!empty($sb)){
			return myerr('一小时内只可以提醒一次');
		}
		$olddata = ApprovalLog::get($idata['Id']);
		if(empty($olddata)){
			return myerr('没找到需要的审批订单');
		}
		$olddata = $olddata->toArray();
		//找到需要调整的规则信息
		$olddata['RulesPlan'] = json_decode($olddata['RulesPlan'],true);
		$lsdata = [];
		//需要审批的 下个节点的标识 
		$nextitemuser = [];
		$successflag = false;
		foreach ($olddata['RulesPlan']['Rules'] as $key => $value) {
			if($value['Step']==$idata['Step']){
				if(empty($value['list'][$idata['ListIndex']])){
					return myerr('没有找到需要审批环节信息');
				}
				$lsdata = $value['list'][$idata['ListIndex']];
				$nextitemuser = $lsdata['UserId'];
			}
		}
		if(empty($lsdata)){
			return myerr('没有找到需要审批环节信息');
		}
		//检测此信息是否已经审批通过了
		if($lsdata['State']=='2'){
			return myerr('此环节已经审批通过');
		}
		Sendmessage::approval(4,$olddata,$nextitemuser);
		Cache::set('Id'.$idata['Id'].'Step'.$idata['Step'].'ListIndex'.$idata['ListIndex'],true,3600);
		return myok('提醒成功');
	}
	
}
